<template>
  <v-data-table :headers="headers" 
  :items="desserts" sort-by="calories" class="elevation-1">
    <template v-slot:top>
      <v-toolbar flat>
        <v-toolbar-title>会计子流水辅表</v-toolbar-title>
        <v-divider class="mx-4" inset vertical></v-divider>
        <v-spacer></v-spacer>
        <v-dialog v-model="dialog" max-width="800px">
          <template v-slot:activator="{ on }">
            <v-btn color="primary" dark class="mb-2" v-on="on">新增</v-btn>
          </template>
          <v-card>
            <v-card-title>
              <span class="headline">{{ formTitle }}</span>
            </v-card-title>

            <v-card-text>
              <v-container>
                <v-row>
                  
                    <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.kltrnseq" label="核心流水号"></v-text-field>
                  </v-col>
                    <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.klseqno" label="核心流水号序号"></v-text-field>
                  </v-col>
                  
                   <v-col cols="12" sm="6" md="4">
                  <v-select :items="prdamtflgItems" 
                  label="产品属性类别" 
                  v-model="editedItem.prdamtflg"
                  item-text="name"
                  item-value="attributes"
                  @change="prdamtflgSel(editedItem)"
                  ></v-select>
                  </v-col>
                    <!-- <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.propdesc" label="属性标记"></v-text-field>
                  </v-col> -->

                   <v-col cols="12" sm="6" md="4">
                  <v-select :items="propdescItems" 
                  label="属性标记" 
                  v-model="editedItem.propdesc"
                  item-text="name"
                  item-value="attributes"
                  ></v-select>
                  </v-col>

                    <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.propval" label="属性值"></v-text-field>
                  </v-col>
                </v-row>
              </v-container>
            </v-card-text>

            <v-card-actions>
              <v-spacer></v-spacer>
              <v-btn color="blue darken-1" text @click="close">取消</v-btn>
              <v-btn color="blue darken-1" text @click="save">保存</v-btn>
            </v-card-actions>
          </v-card>
        </v-dialog>
      </v-toolbar>
    </template>
    <template v-slot:item.actions="{ item }">
      <v-icon small class="mr-2" @click="editItem(item)">mdi-pencil</v-icon>
      <v-icon small @click="deleteItem(item)">mdi-delete</v-icon>
    </template>

    <template v-slot:item.rcdsts="{ item }">
    
      <span v-for="s in rcdstsItems" key="s.id" v-if="s.attributes==item.rcdsts">{{s.name}}</span>
    </template>

     <template v-slot:item.prdpropdes="{ item }">
    
      <span v-for="s in prdpropcodItems" key="s.id" v-if="s.attributes==item.prdpropcod">{{s.name}}</span>
    </template>
    <template v-slot:no-data>
      <v-btn color="primary" @click="initialize">Reset</v-btn>
    </template>
  </v-data-table>
</template>

<script>
export default {
  data: () => ({
    prdamtflgItems: [],
    accfacennmItems:[],
    proAttributesItems:[],
    propdescItems:[],
    dialog: false,
    headers: [
      
      { text: "核心流水号", value: "kltrnseq" },
      { text: "核心流水号序号", value: "klseqno" },
      { text: "产品金额标志", value: "prdamtflg" },
      { text: "属性标记", value: "propdesc" },
       { text: "属性值", value: "propval" },
      { text: "操作", value: "actions", sortable: false }
    ],
    desserts: [],
    editedIndex: -1,
    editedItem: {
      kltrnseq: "",
      klseqno: "",
      prdamtflg:"",
      propdesc: "",
      propval:"",
    },
    defaultItem: {
     kltrnseq: "",
      klseqno: "",
      prdamtflg:"",
      propdesc: "",
      propval:"",
    }
  }),

  computed: {
    formTitle() {
      return this.editedIndex === -1 ? "新增" : "修改";
    }
  },

  watch: {
    dialog(val) {
      val || this.close();
    }
  },

  created() {
    this.initialize();
    this.getDict()
  },

  methods: {
    initialize() {
     this.axios
        .get("/acfe17/list")
        .then(response => {
          this.desserts = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
    },
    getDict(){

       
        this.axios
        .get("/dict/topName/产品属性类别")
        .then(response => {
          this.prdamtflgItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });

        this.axios
        .get("/dict/topName/产品属性")
        .then(response => {
          this.proAttributesItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });

        this.axios
        .get("/dict/topName/记账要素")
        .then(response => {
          this.accfacennmItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });

        
       
    },
    editItem(item) {
      this.editedIndex = this.desserts.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog = true;
    },

    deleteItem(item) {
      const index = this.desserts.indexOf(item);
      confirm("确定要删除此条记录吗?") &&
        this.axios.delete('/acfe17/'+item.id)
          .then(res=>{
            this.desserts.splice(index, 1);
          })
        
    },

    close() {
      this.dialog = false;
      this.$nextTick(() => {
        this.editedItem = Object.assign({}, this.defaultItem);
        this.editedIndex = -1;
      });
    },

    save() {
       this.axios.post("/acfe17", this.$qs.stringify(this.editedItem)).then(res => {
          this.initialize();
          this.close();
      });
      
    },
    prdamtflgSel(item){
      console.log(item)
      if(item.prdamtflg=='1') this.propdescItems=this.proAttributesItems
      if(item.prdamtflg=='2') this.propdescItems=this.accfacennmItems
    }
  }
};
</script>